package com.ziyun.erp.modules.order.listener;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cloud.bus.event.order.ErpOrderPayEvent;
import com.cloud.bus.event.order.OrderCancelBatchEvent;
import com.utility.JsonUtils;
import com.ziyun.erp.modules.order.service.ErpOrderService;
import com.ziyun.erp.modules.supp.entity.SellerInfoEntity;
import com.ziyun.erp.modules.supp.service.SellerInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 订单事件监听
 *
 * @author zh
 * @date 2019/1/149:53
 */
@Slf4j
@Component
public class OrderEventListener {
    @Autowired
    private ErpOrderService erpOrderService;
    @Autowired
    private SellerInfoService sellerInfoService;

    /**
     * 自营门店/三方门店订单 同步到erp-order
     *
     * @param event
     * @throws Exception
     */
    @EventListener(classes = ErpOrderPayEvent.class)
    public void handlerOrderCopyEvent(ErpOrderPayEvent event) throws Exception {
        log.info("同步订单order_info -> erp_order event [{}] = [{}] = [{}]",
                event.getClass().getName(),
                JsonUtils.object2String(event.getSellerIds()),
                JsonUtils.object2String(event.getOrderIds())
        );

        List<SellerInfoEntity> sellerInfoList = sellerInfoService.selectList(new EntityWrapper<SellerInfoEntity>().in("seller_id", event.getSellerIds()));

        for (SellerInfoEntity sellerInfoEntity : sellerInfoList) {
            if (sellerInfoEntity.getSellerId() == 1 || sellerInfoEntity.getSellerId() == 18 || sellerInfoEntity.getSellerId() == 20) {
                continue;
            }
            log.info("========== 商城订单：" + sellerInfoEntity.getSellerName() + " ,转门店生产 开始 ========== ");

            erpOrderService.synProduce(sellerInfoEntity.getSellerId(), sellerInfoEntity.getSelf() == 0 ? 1 : 0);

            log.info("========== 商城订单：" + sellerInfoEntity.getSellerName() + " ,转门店生产 结束 ========== ");

        }

    }

}
